using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using Rilisoft.MiniJson;
using UnityEngine;

public class InAppSubscriptionsControllerOLD : MonoBehaviour
{
	[Serializable]
	public class InAppPromo
	{
		private bool enabled;

		public string subscriptionId = string.Empty;

		public int amount;

		public int startTime;

		public int endTime;

		public string titleEn = string.Empty;

		public string titleRus = string.Empty;

		public string descriptionEn = string.Empty;

		public string descriptionRus = string.Empty;

		public string packsLeftEn = string.Empty;

		public string packsLeftRus = string.Empty;

		public bool isCashPromo = true;

		public int inAppPar = -1;

		public string showForNewOrOldUser = string.Empty;

		public string showForPayingOrNotUser = string.Empty;

		public int hoursFromStart;

		public int neededLeague;

		public bool isSingleUse;

		public string uniqueKey = string.Empty;

		public bool Enabled
		{
			get
			{
				bool flag = false;
				bool flag2 = false;
				bool flag3 = false;
				bool flag4 = PromoActionManager.NewUser(PromoActionManager.fyber.newUserStatusHours);
				if ((flag4 && showForNewOrOldUser == "new") || (!flag4 && showForNewOrOldUser == "old") || showForNewOrOldUser == string.Empty)
				{
					flag2 = true;
				}
				if ((PromoActionManager.PayingUser() && showForPayingOrNotUser == "payed") || (!PromoActionManager.PayingUser() && showForPayingOrNotUser == "non_payed") || showForPayingOrNotUser == string.Empty)
				{
					flag = true;
				}
				flag3 = PromoActionManager.PlayedHours(hoursFromStart);
				return enabled && PromoActionManager.CurrentUnixTimeSecond > startTime && PromoActionManager.CurrentUnixTimeSecond < endTime && (flag || flag2) && flag3 && neededLeague <= CupsManager.CurrentTier && (!isSingleUse || (isSingleUse && !BCWStorage.Get<bool>(uniqueKey)));
			}
			set
			{
				enabled = value;
			}
		}

		public string GetTitle()
		{
			return (!(LocalizationManager.CurrentLanguage == "Russian")) ? titleEn : titleRus;
		}

		public string GetDescription()
		{
			return (!(LocalizationManager.CurrentLanguage == "Russian")) ? descriptionEn : descriptionRus;
		}

		public string GetPacksLeftTitle()
		{
			return GetPacksLeftTitle("ffff00ff");
		}

		public string GetPacksLeftTitle(string bbcode)
		{
			int packsLeftAmount = GetPacksLeftAmount();
			string arg = string.Format("[{0}]", bbcode) + packsLeftAmount + "[-]";
			return (!(LocalizationManager.CurrentLanguage == "Russian")) ? string.Format(packsLeftEn, arg) : string.Format(packsLeftRus, arg);
		}

		public int GetPacksLeftAmount()
		{
			int num = amount;
			int currentUnixTimeSecond = PromoActionManager.CurrentUnixTimeSecond;
			int num2 = startTime;
			int num3 = endTime;
			int num4 = Mathf.CeilToInt((1f - (float)(currentUnixTimeSecond - num2) / (float)(num3 - num2)) * (float)num);
			return (num4 <= 0) ? 1 : num4;
		}
	}

	[Serializable]
	public class SubscriptionParameters
	{
		public int maxDays;

		public int cycleHourse;

		public int startCash;

		public int startGold;

		public int cycleReward;
	}

	private const string KEY_SUBSCRIPTION_START_TIME = "subscriptionStartTime";

	private const string KEY_REWARD_START_TIME = "REWARDStartTime";

	private const string KEY_SUBSCRIPTION_ID = "subscriptionId";

	private static InAppSubscriptionsControllerOLD _instance;

	private bool _isRequestAndLoadRunning;

	public bool jsonReaded;

	private InAppPromo promo = new InAppPromo();

	private SubscriptionParameters currentSubscription = new SubscriptionParameters();

	private string currentId;

	public static InAppSubscriptionsControllerOLD instance
	{
		get
		{
			if (_instance == null)
			{
				GameObject gameObject = new GameObject();
				gameObject.name = "InAppPromoController(Don't Destroy)";
				gameObject.AddComponent<InAppPromoController>();
			}
			return _instance;
		}
	}

	public static event Action OnStartTimer;

	public static event Action OnStartSubscription;

	public static event Action OnEndSubscription;

	public static event Action OnJSONLoaded;

	private void Awake()
	{
		if (_instance != null)
		{
			UnityEngine.Object.DestroyImmediate(this);
			return;
		}
		_instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	private void Start()
	{
		currentId = BCWStorage.Get<string>("subscriptionId");
		StartCoroutine(RequestAndLoadPromoJson());
	}

	public bool TryTakeReward()
	{
		if (BCWStorage.Get<int>("REWARDStartTime") + currentSubscription.cycleHourse * 3600 <= JSONManager.Instance.GetServerTime())
		{
			settings.CathedWarnings += currentSubscription.cycleReward;
			settings.SaveCathedExeptionsToStorager();
			if (BCWStorage.Get<int>("subscriptionStartTime") + currentSubscription.maxDays > JSONManager.Instance.GetServerTime())
			{
				StartTimer();
			}
			else
			{
				EndSubscription();
			}
			return true;
		}
		return false;
	}

	public bool GetSubscriptionState()
	{
		return !string.IsNullOrEmpty(currentId);
	}

	public int GetCycleRemaininTime()
	{
		return BCWStorage.Get<int>("REWARDStartTime") + currentSubscription.cycleHourse * 3600 - JSONManager.Instance.GetServerTime();
	}

	public int GetSubscriptionRemaininDays()
	{
		return (BCWStorage.Get<int>("subscriptionStartTime") + currentSubscription.maxDays * 86400 - JSONManager.Instance.GetServerTime()) / 86400;
	}

	public void StartSubscription()
	{
		currentId = promo.subscriptionId;
		BCWStorage.Set("subscriptionId", promo.subscriptionId, true);
		settings.CathedWarnings += currentSubscription.startGold;
		settings.CathedExeptions += currentSubscription.startCash;
		settings.SaveCathedExeptionsToStorager();
		if (InAppSubscriptionsControllerOLD.OnStartSubscription != null)
		{
			InAppSubscriptionsControllerOLD.OnStartSubscription();
		}
		StartTimer();
	}

	private void EndSubscription()
	{
		BCWStorage.Set("subscriptionId", string.Empty, true);
		BCWStorage.Set("subscriptionStartTime", 0, true);
		if (InAppSubscriptionsControllerOLD.OnEndSubscription != null)
		{
			InAppSubscriptionsControllerOLD.OnEndSubscription();
		}
	}

	private void StartTimer()
	{
		int serverTime = JSONManager.Instance.GetServerTime();
		BCWStorage.Set("REWARDStartTime", serverTime);
		if (InAppSubscriptionsControllerOLD.OnStartTimer != null)
		{
			InAppSubscriptionsControllerOLD.OnStartTimer();
		}
	}

	private IEnumerator RequestAndLoadPromoJson()
	{
		string jsonText = JSONManager.Instance.GetJSON(URLs.InAppSubscriptions);
		string oldJson = string.Empty;
		while (jsonText == oldJson)
		{
			yield return new WaitForSeconds(3f);
			oldJson = jsonText;
			jsonText = JSONManager.Instance.GetJSON(URLs.InAppSubscriptions);
		}
		object fgsfsdf = Json.Deserialize(jsonText);
		Dictionary<string, object> parsedJson = Json.Deserialize(jsonText) as Dictionary<string, object>;
		if (parsedJson == null)
		{
			_isRequestAndLoadRunning = false;
			yield break;
		}
		if (string.IsNullOrEmpty(currentId))
		{
			if (parsedJson.ContainsKey("promo"))
			{
				Dictionary<string, object> inAppPromoObject = parsedJson["promo"] as Dictionary<string, object>;
				string subscriptionId = ParsePromoObject(inAppPromoObject);
				ParseSubscription(parsedJson[subscriptionId] as Dictionary<string, object>);
			}
			else
			{
				BCWDebug.Log("InApp Promo response doesn't contain “promos” property.", "Promo");
			}
		}
		else
		{
			ParseSubscription(parsedJson[currentId] as Dictionary<string, object>);
		}
		_isRequestAndLoadRunning = false;
		jsonReaded = true;
		if (InAppSubscriptionsControllerOLD.OnJSONLoaded != null)
		{
			InAppSubscriptionsControllerOLD.OnJSONLoaded();
		}
	}

	private string ParsePromoObject(Dictionary<string, object> inAppPromoObj)
	{
		if (inAppPromoObj == null)
		{
			return string.Empty;
		}
		string str = string.Empty;
		new List<string>(inAppPromoObj.Keys).ForEach(delegate(string x)
		{
			str = str + x + "\n";
		});
		Debug.LogError(str);
		promo.Enabled = Convert.ToInt32(inAppPromoObj["enabled"]) == 1;
		promo.titleEn = (string)inAppPromoObj["titleEn"];
		promo.titleRus = (string)inAppPromoObj["titleRus"];
		promo.descriptionEn = (string)inAppPromoObj["descriptionEn"];
		promo.descriptionRus = (string)inAppPromoObj["descriptionRus"];
		promo.packsLeftEn = (string)inAppPromoObj["packsLeftEn"];
		promo.packsLeftRus = (string)inAppPromoObj["packsLeftRus"];
		promo.inAppPar = Convert.ToInt32((!inAppPromoObj.ContainsKey("inAppPar")) ? ((object)(-1)) : inAppPromoObj["inAppPar"]);
		promo.amount = Convert.ToInt32(inAppPromoObj["totalAmount"]);
		promo.startTime = Convert.ToInt32(inAppPromoObj["startTime"]);
		promo.endTime = Convert.ToInt32(inAppPromoObj["endTime"]);
		promo.showForNewOrOldUser = (string)((!inAppPromoObj.ContainsKey("newOldUser")) ? string.Empty : inAppPromoObj["newOldUser"]);
		promo.showForPayingOrNotUser = (string)((!inAppPromoObj.ContainsKey("payedNonPayedUser")) ? string.Empty : inAppPromoObj["payedNonPayedUser"]);
		promo.hoursFromStart = Convert.ToInt32((!inAppPromoObj.ContainsKey("hoursFromStart")) ? ((object)0) : inAppPromoObj["hoursFromStart"]);
		promo.neededLeague = Convert.ToInt32((!inAppPromoObj.ContainsKey("needLeague")) ? ((object)0) : inAppPromoObj["needLeague"]);
		promo.isSingleUse = inAppPromoObj.ContainsKey("singleUse") && Convert.ToInt32(inAppPromoObj["singleUse"]) == 1;
		promo.subscriptionId = (string)((!inAppPromoObj.ContainsKey("id")) ? string.Empty : inAppPromoObj["id"]);
		promo.uniqueKey = promo.startTime + promo.subscriptionId + promo.endTime;
		return promo.subscriptionId;
	}

	private void ParseSubscription(Dictionary<string, object> subscription)
	{
		if (subscription != null)
		{
			currentSubscription.maxDays = Convert.ToInt32((!subscription.ContainsKey("maxDays")) ? ((object)(-1)) : subscription["maxDays"]);
			currentSubscription.cycleHourse = Convert.ToInt32((!subscription.ContainsKey("cycleHourse")) ? ((object)(-1)) : subscription["cycleHourse"]);
			currentSubscription.startCash = Convert.ToInt32((!subscription.ContainsKey("startCash")) ? ((object)(-1)) : subscription["startCash"]);
			currentSubscription.startGold = Convert.ToInt32((!subscription.ContainsKey("startGold")) ? ((object)(-1)) : subscription["startGold"]);
			currentSubscription.cycleReward = Convert.ToInt32((!subscription.ContainsKey("cycleReward")) ? ((object)(-1)) : subscription["cycleReward"]);
		}
	}
}
